import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mcdonalds/lblbc_constants.dart';
import 'package:mcdonalds/lblbc_ui_kit.dart';
import 'package:mcdonalds/network/bean/add_to_cart_req_entity.dart';
import 'package:mcdonalds/network/bean/create_order_req_entity.dart';
import 'package:mcdonalds/network/bean/query_goods_detail_resp_entity.dart';
import 'package:mcdonalds/network/http_manager.dart';
import 'package:mcdonalds/pages/login/login.dart';
import 'package:mcdonalds/pages/login/login_manager.dart';
import 'package:mcdonalds/pages/order/order_confirm.dart';

/// 厦门大学计算机专业 | 前华为工程师
/// 专注《零基础学编程系列》  http://lblbc.cn/blog
/// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
/// 公众号：蓝不蓝编程

class GoodsPage extends StatefulWidget {
  final String goodsId;

  const GoodsPage(this.goodsId, {Key? key}) : super(key: key);

  @override
  createState() => _GoodsState();
}

class _GoodsState extends State<GoodsPage> {
  QueryGoodsDetailRespData? queryGoodsDetailRespData;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: LblColors.mainColor,
      ),
      body: Container(
          margin: const EdgeInsets.fromLTRB(5, 20, 10, 0),
          child: Column(
            children: [
              Expanded(
                  child: Column(children: [
                buildGoodsInfoBlock(),
              ])),
              lblDivider(),
              buildBottomRow()
            ],
          )),
    );
  }

  buildGoodsInfoBlock() {
    if (queryGoodsDetailRespData == null) {
      return emptyContainer();
    } else {
      return Column(
        children: [
          Image(
            image: NetworkImage(queryGoodsDetailRespData!.squarePic),
            width: 200,
            height: 200,
          ),
          lblColumnSpacer(10),
          Card(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  child: Text("￥${queryGoodsDetailRespData!.price / 100}",
                      style: const TextStyle(fontSize: 16.0, color: Color(0xFFED9220))),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 10),
                  child:
                      Text(queryGoodsDetailRespData!.name, style: const TextStyle(fontSize: 16.0, color: Colors.black)),
                ),
                lblColumnSpacer(10),
              ],
            ),
          ),
          lblColumnSpacer(10),
          Card(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: Text(queryGoodsDetailRespData!.description,
                      style: const TextStyle(fontSize: 16.0, color: Colors.black)),
                ),
                lblColumnSpacer(10),
              ],
            ),
          )
        ],
      );
    }
  }

  Container buildBottomRow() {
    return Container(
      margin: const EdgeInsets.only(bottom: 10),
      child: Center(
        child: ElevatedButton(
          child: const Text('下单', style: TextStyle(color: Colors.white)),
          onPressed: () {
            gotoConfirmOrderPage();
          },
        ),
      ),
    );
  }

  void gotoConfirmOrderPage() {
    if (queryGoodsDetailRespData != null) {
      CreateOrderReqSimpleCartItemList createOrderReqSimpleCartItem = CreateOrderReqSimpleCartItemList();
      createOrderReqSimpleCartItem.goodsId = widget.goodsId;
      createOrderReqSimpleCartItem.quantity = 1;
      List<CreateOrderReqSimpleCartItemList> simpleCartItemList = List.filled(1, createOrderReqSimpleCartItem);

      Navigator.push(context, MaterialPageRoute(builder: (context) => OrderConfirmPage(simpleCartItemList)));
    }
  }

  @override
  void initState() {
    super.initState();
    _queryData();
  }

  _queryData() async {
    String url = "mcdonalds/goods/" + widget.goodsId;
    HttpManager.getInstance().get(url).then((resp) {
      var result = QueryGoodsDetailRespEntity.fromJson(resp);
      setState(() {
        queryGoodsDetailRespData = result.data;
      });
    });
  }

  void addToCart() {
    LoginManager.isLoggedIn().then((value) {
      if (value) {
        String url = "mcdonalds/cart";
        AddToCartReqEntity request = AddToCartReqEntity();
        request.goodsId = widget.goodsId;
        HttpManager.getInstance().post(url, data: request.toJson()).then((resp) {
          Fluttertoast.showToast(
            msg: "已加入购物车",
          );
        });
      } else {
        Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage()));
      }
    });
  }
}
